﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

using EnvDTE;
using EnvDTE100;

namespace Artisteer_Project_Converter.VisualStudioApi
{
    public class VsProjectItem
    {
        private ProjectItem projectItem = null;

        /// <summary>
        /// Create project instance
        /// </summary>
        /// <param name="solution">Project object</param>
        internal VsProjectItem(ProjectItem projectItem)
        {
            this.projectItem = projectItem;
        }

        /// <summary>
        /// Open code editor
        /// </summary>
        /// <returns></returns>
        public VsWindow OpenCodeEditor()
        {
            while (true)
            {
                try
                {
                    return new VsWindow(projectItem.Open(EnvDTE.Constants.vsViewKindCode));
                }
                catch (COMException ex)
                {
                    if (ex.ErrorCode == -2147418111)
                        System.Threading.Thread.Sleep(100);
                    else
                        throw ex;
                }
            }
        }

        /// <summary>
        /// Open designer
        /// </summary>
        /// <returns></returns>
        public VsWindow OpenDesigner()
        {
            while (true)
            {
                try
                {
                    return new VsWindow(projectItem.Open(EnvDTE.Constants.vsViewKindDesigner));
                }
                catch (COMException ex)
                {
                    if (ex.ErrorCode == -2147418111)
                        System.Threading.Thread.Sleep(100);
                    else
                        throw ex;
                }
            }
        }

        /// <summary>
        /// Open code editor
        /// </summary>
        /// <returns></returns>
        public VsWindow OpenTextEditor()
        {
            while (true)
            {
                try
                {
                    return new VsWindow(projectItem.Open(EnvDTE.Constants.vsViewKindTextView));
                }
                catch (COMException ex)
                {
                    if (ex.ErrorCode == -2147418111)
                        System.Threading.Thread.Sleep(100);
                    else
                        throw ex;
                }
            }
        }
    }
}
